package com.taotao.search.controller;

import java.io.UnsupportedEncodingException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.taotao.pojo.SearchResult;
import com.taotao.search.service.SearchService;

/**
 * 搜索服务Controller
 * @author wsy
 *
 */
@Controller
public class SearchController {

	@Autowired
	private SearchService searchService;
	
	@RequestMapping("/search")
	public String showIndex(@RequestParam("q")String queryString,@RequestParam(defaultValue="1")Integer page,Model model){
		//把查询条件进行转码，解决get乱码问题
		try {
			queryString=new String(queryString.getBytes("ISO8859-1"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		SearchResult result=searchService.search(queryString, page, 60);
		System.out.println(result);
		model.addAttribute("query",queryString);
		model.addAttribute("totalPages",result.getTotalPages());
		model.addAttribute("itemList",result.getItemList()); 
		model.addAttribute("page",page);
		//返回逻辑视图
		return "search";
	}
}
